package com.ruoyi.cderp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.cderp.mapper.BaseDataCustomerMapper;
import com.ruoyi.cderp.domain.BaseDataCustomer;
import com.ruoyi.cderp.service.IBaseDataCustomerService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 客户Service业务层处理
 *
 * @author ruoyi
 * @date 2025-02-17
 */
@Service
public class BaseDataCustomerServiceImpl implements IBaseDataCustomerService
{
    @Autowired
    private BaseDataCustomerMapper baseDataCustomerMapper;

    LoginUser loginUser = null;

    /**
     * 查询客户
     *
     * @param id 客户主键
     * @return 客户
     */
    @Override
    public BaseDataCustomer selectBaseDataCustomerById(String id)
    {
        return baseDataCustomerMapper.selectBaseDataCustomerById(id);
    }

    /**
     * 查询客户列表
     *
     * @param baseDataCustomer 客户
     * @return 客户
     */
    @Override
    public List<BaseDataCustomer> selectBaseDataCustomerList(BaseDataCustomer baseDataCustomer)
    {
        return baseDataCustomerMapper.selectBaseDataCustomerList(baseDataCustomer);
    }

    /**
     * 新增客户
     *
     * @param baseDataCustomer 客户
     * @return 结果
     */
    @Override
    public int insertBaseDataCustomer(BaseDataCustomer baseDataCustomer)
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String formattedDate = sdf.format(new Date());
        Random random = new Random();
        int randomNumber = 10000000 + random.nextInt(90000000); // 生成8位数随机数
        baseDataCustomer.setId("10" + formattedDate+randomNumber);
        baseDataCustomer.setCreateTime(DateUtils.getNowDate());
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataCustomer.setCreateById(loginUser.getUser().getUserId().toString());
        baseDataCustomer.setCreateBy(loginUser.getUser().getUserName());
        baseDataCustomer.setCreateTime(DateUtils.getNowDate());
        return baseDataCustomerMapper.insertBaseDataCustomer(baseDataCustomer);
    }

    /**
     * 修改客户
     *
     * @param baseDataCustomer 客户
     * @return 结果
     */
    @Override
    public int updateBaseDataCustomer(BaseDataCustomer baseDataCustomer)
    {
        // 获取当前登录用户对象
        loginUser = SecurityUtils.getLoginUser();
        baseDataCustomer.setUpdateById(loginUser.getUser().getUserId().toString());
        baseDataCustomer.setUpdateBy(loginUser.getUser().getUserName());
        baseDataCustomer.setUpdateTime(DateUtils.getNowDate());
        return baseDataCustomerMapper.updateBaseDataCustomer(baseDataCustomer);
    }

    /**
     * 批量删除客户
     *
     * @param ids 需要删除的客户主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataCustomerByIds(String[] ids)
    {
        return baseDataCustomerMapper.deleteBaseDataCustomerByIds(ids);
    }

    /**
     * 删除客户信息
     *
     * @param id 客户主键
     * @return 结果
     */
    @Override
    public int deleteBaseDataCustomerById(String id)
    {
        return baseDataCustomerMapper.deleteBaseDataCustomerById(id);
    }
}
